package com.zkingsoft.services.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.dao.ProjUserDao;
import com.zkingsoft.dao.ProjectDao;
import com.zkingsoft.model.ProjUser;
import com.zkingsoft.model.Project;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.services.ProjectService;

/**
 * This field was generated by Zking.software.Codegen.
 * 
 * @date 2016-11-09 09:41
 */
@Service("projectService")
public class ProjectServiceImpl implements ProjectService {

	@Autowired
	private ProjectDao projectDao;
	
	@Autowired
	private ProjUserDao projUserDao;
	
	@Transactional
	@Override
	public int add(Project project) {
		project.setStatus(Dictionary.PROJ_STATUS_XQDY);
		Users users=(Users) WebUtil.getSession().getAttribute(SystemConstance.LOGIN_KEY);
		project.setUserId(users.getId());
		project.setCreateTime(new Date());
		int i = projectDao.insert(project);
		updateProjUser(project);
		return i;
	}
	@Override
	public int modify1(Project project) {
		return projectDao.update(project);

	}
	
	@Transactional
	@Override
	public int modify(Project project) {
		updateProjUser(project);
		return projectDao.update(project);

	}

	@Override
	public int remove(List<Long> list) {
		return projectDao.deleteByIds(list);
	}
	
	/**
	* @Description: 更新项目中的用户
	* @author:姜友瑶
	* @param project    
	* 返回类型  void    
	* @date 2016年11月11日
	 */
	private void updateProjUser(Project project) {
		//新增项目用户
		List<ProjUser> projUserList=project.getProjUserList();
		if(projUserList==null || projUserList.size()==0){
			return ;
		}
		for (ProjUser projUser : projUserList) {
			if(projUser.getUserId()==null){
				continue;
			}
			if(projUser.getId()!=null){
				projUserDao.update(projUser);
				continue;
			}
			
			projUser.setProjId(project.getId());
			projUserDao.insert(projUser);
		}
	}

	@Override
	public int removeById(Long id) {

		return projectDao.deleteById(id);

	}

	@Override
	public int removeByModel(Project project) {

		return projectDao.deleteByModel(project);

	}

	@Override
	public List<Project> findInPage(Project project, PaginationVO pageVo) {

		return projectDao.selectInPage(project, pageVo);

	}

	@Override
	public List<Project> findByModel(Project project) {

		return projectDao.selectByModel(project);

	}

	@Override
	public int findTotal(Project project) {

		return projectDao.selectTotalRecord(project);

	}

	@Override
	public Project findById(Long id) {

		return projectDao.selectById(id);

	}
	/**
	 * 根据员工id 查询与员工相关的项目。
	 */
	@Override
	public List<Project> findByUserId(Long id,Project project, PaginationVO pageVo) {
		// TODO Auto-generated method stub
		return projectDao.findByUserIdInpage(id,project, pageVo);
	}

	@Override
	public Integer findByUserIdCount(Long id, Project project) {
		// TODO Auto-generated method stub
		return projectDao.findByUserIdCount(id,project);
	}
	@Override
	public List<Long> selectProjectId() {
		return projectDao.selectProjectId();
	}

}